<?php
/*
 *@des 后台导航菜单管理
*@author www.speakphp.com
*/
class NavAction extends BaseAction {
	public $nav_color = array(
			0=>'#007296' ,
			1=>'#3E4452' ,
			2=>'green' ,
			3=>"red" ,
			4=>'#056DAE',
				
	) ;
	public function __construct(){
		parent::__construct();
	}
	public function index(){
		$d = D("Admin/Nav");
		$list = $d->getNavList(" AND status in (1, 0 ) ");
		$list = tree_format($list , 0 , 0 , '&nbsp;&nbsp;├─');
		/* echo "<pre>";
		print_r($list); */
		$this->assign("list" , $list) ;
		$this->assign("nav_color" , $this->nav_color) ;
		$this->display();
	}
	//添加导航
	function add(){
		$d = D("Admin/Nav");
		$action = $this->_request("action");
		$action_array = array("show","doadd");
		$action = !in_array($action,$action_array)?'show':$action ;
		
		if($action == 'show'){
			$id = intval($this->_request("id"));
			$result = $d->queryList("childs");
			$options =  getChildren($result);
			/* echo "<pre>";
			print_r($options); */
			$this->assign("options" , $options) ;
			
			$this->assign("pid" , $id) ;
			$this->display();
				
		}elseif($action == 'doadd'){
			$this->doadd();
		}
		
	}
	//处理导航添加
	public function doadd(){
		$pid = intval($this->_request("pid"));
		$name = trim($this->_request("name"));
		$disorder = intval($this->_request("disorder"));
		$url = trim($this->_request("url"));
		$status = intval($this->_request("status"));
		$d = D("Admin/Nav");
		$insert = array(
				'pid'=>$pid , 
				'name'=>$name ,
				'disorder'=>$disorder ,
				'url'=>$url ,
				'status'=>$status ,
		);
		$re = $d->addNav($insert);
		$url = U("nav/index") ;
		if($re['status']){
			echo "<script>parent.window.location.href='{$url}';</script>";
		}else{
			throw_exception($re['message']);
		}
	}
	//修改导航
	function edit(){
		$d = D("Admin/Nav");
		$action = $this->_request("action");
		$action_array = array("show","doedit" , "do_disorder");
		$action = !in_array($action,$action_array)?'show':$action ;
	
		if($action == 'show'){
			$id = intval($this->_request("id"));
			$result = $d->queryList("childs");
			$options =  getChildren($result);
			$info = $d->getNavInfo($id);
			$this->assign("info" , $info) ;
			$this->assign("options" , $options) ;
			$this->assign("pid" , $id) ;
			$this->display();
	
		}elseif($action == 'doedit'){
			$this->doedit();
		}elseif($action == "do_disorder"){
			$this->do_disorder();
		}
	
	}
	private function do_disorder(){
		$id = intval($this->_request("id"));
		$disorder = intval($this->_request("disorder"));	
		$d = D("Admin/Nav");
		$update = array(
				'disorder'=>$disorder ,
		);
		$re = $d->do_disorder($update , $id );
		$url = U("nav/index") ;
		if($re['status']){
			echo result_to_towf_new('' , 1 , '修改成功' ,'');
		}else{
			echo result_to_towf_new('' , 0,$re['message'],'');
		}
	}
	//处理导航添加
	public function doedit(){
		$pid = intval($this->_request("pid"));
		$id = intval($this->_request("id"));
		$name = trim($this->_request("name"));
		$disorder = intval($this->_request("disorder"));
		$url = trim($this->_request("url"));
		$status = intval($this->_request("status"));
		$d = D("Admin/Nav");
		$update = array(
				'pid'=>$pid ,
				'name'=>$name ,
				'disorder'=>$disorder ,
				'url'=>$url ,
				'status'=>$status ,
		);
		$re = $d->editNav($update , $id );
		$url = U("nav/index") ;
		if($re['status']){
			echo "<script>parent.window.location.href='{$url}';</script>";
		}else{
			throw_exception($re['message']);
		}
	}
	//导航删除
	function del(){
		$d = D("Admin/Nav");
		$id = intval($this->_request("id"));
		$status = $d->delNav($id);
		if($status){
			echo result_to_towf_new(null, 1, "删除 成功", null);
			exit();
		}else{
			echo result_to_towf_new(null, 0, "删除 失败， 服务器繁忙", null);
			exit();
		}
	}
		
}
